package adv10;

import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class XML2 {
    public static final File XMLFILE = new File("E:\\java_workspace\\IdeaProjects\\java86\\javaAdvance\\src\\adv9" +
            "\\book.xml");

    //public static final File XMLFILE=new File("d:/tmp/book.xml");
    public static void main(String[] args) {
        //        search("1");
        // addNewBook("6", 3, "java web开发", 100.5, "把java和html结合起来");
       // modify("6", "高级javaweb开发", 299);
        //delete("javaoop");
        delete("java高级特性");
    }

    //根据书名删除book
    public static void delete(String name) {
//
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLFILE);
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        //xpath的查询语法
        List<Node> nodes = doc.selectNodes("/books/book[name='" + name + "']");
        if (nodes.size() == 0) {
            System.out.println("没有找到" + name);
            return;
        }
        for (Node node : nodes) {
            node.getParent().remove(node);
        }
        try {
            XMLWriter writer = new XMLWriter(new FileWriter(XMLFILE));
            writer.write(doc);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //根据index 属性 查询 book信息
    public static void search(String index) {
        //
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLFILE);
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        //xpath的查询语法
        List<Node> nodes = doc.selectNodes("/books/book[@index='" + index + "']");
        if (nodes.size() == 0) {
            System.out.println("没有找到");
            return;
        }
        for (Node n : nodes) {
            Element e = (Element) n;
            String diff = e.attributeValue("diff");
            String name = e.selectSingleNode("name").getText();
            String price = e.selectSingleNode("price").getText();
            String desc = e.selectSingleNode("desc").getText().trim();

            System.out.println(diff + "," + name + "," + price + "," + desc);
        }

    }

    public static void modify(String index, String newName, double newPrice) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLFILE);
        } catch (Exception e) {
            return;
        }
        Element e = (Element) doc.selectSingleNode("/books/book[@index='" + index + "']");
        if (e == null) {
            System.out.println("没有找到这个序号：" + index);
            return;
        }
        e.selectSingleNode("name").setText(newName);
        e.selectSingleNode("price").setText(String.valueOf(newPrice));
        try {

            XMLWriter xm = new XMLWriter(new FileWriter(XMLFILE));
            xm.write(doc);
            xm.close();
            System.out.println("修改成功");
        } catch (Exception ee) {
            ee.printStackTrace();
        }

    }

    //增加
    public static void addNewBook(String no, int diff, String name, double price, String desc) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XMLFILE);
        } catch (Exception e) {
            e.printStackTrace();
            //没有，增加一个xml,增加一个根节点
            doc = DocumentHelper.createDocument();
            doc.addElement("books");
        }
        Element root = doc.getRootElement();
        Element nbook = root.addElement("book");
        nbook.addAttribute("index", no);
        nbook.addAttribute("diff", String.valueOf(diff));
        nbook.addElement("name").setText(name);
        nbook.addElement("price").setText(String.valueOf(price));
        nbook.addElement("desc").setText(desc);
        //写回xml文件
        try {

            XMLWriter xm = new XMLWriter(new FileWriter(XMLFILE));
            xm.write(doc);
            xm.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
